package com.googlecode.dgwt.client.dijit.view.model;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.dom.client.Node;
import com.googlecode.dgwt.client.dojo.data.model.StoreColumnBase;

public class CheckBoxSelector<Item> extends ColumnSelector<Item> {

    public CheckBoxSelector() {
        super();
    }

    public CheckBoxSelector(String title) {
        super(title);
    }

    public CheckBoxSelector(String title, String width) {
        super(title, width);
    }

    public CheckBoxSelector(StoreColumnBase<Item, ?> column) {
        super(column);
    }
    
    protected final String getUniqueId(){
        return "dijitSelectorInput_" + System.identityHashCode(this);
    }

    @Override
    protected Element getCellContent(Element cellElement){
        Node node = cellElement.getFirstChild();
        if (node != null 
                && Element.is(node) 
                && node.<InputElement>cast().getPropertyBoolean(getUniqueId())){
            return node.cast();
        }
        return null;
    }
    
    @Override
    protected Element generateCellContent(Element cellElement) {
        Element inputElement = createInputElement();
        inputElement.setPropertyInt("tabIndex", -1);
        inputElement.setPropertyBoolean(getUniqueId(), true);
        cellElement.appendChild(inputElement);
        return inputElement;
    }
    
    protected InputElement createInputElement(){
        return Document.get().createCheckInputElement();
    }

    @Override
    protected void setCellChecked(Node content, boolean checked) {
        content.<InputElement>cast().setPropertyBoolean("checked", checked);
    }

    @Override
    protected void setCellText(Node content, String displayValue) {
        content.<InputElement>cast().setValue(displayValue);
    }

}
